
#include <QtGui>
#include <sstream>
#include <cstdlib>
#include "mainwin.h"
#include "dialogs.h"

MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
{
    setupUi(this);
    this->setWindowTitle(QString("biblio - *NEW*"));
    view_state = ViewState_NEW_UNCHANGED;
    std::string database_home("./");
    std::string database_name("my_db.db");
    db = new BiblioDb(database_home, database_name);
}

void MainWin::load_data(biblio::Paper *paper)
{
    QListWidgetItem *new_author;
    std::cout << "load_data\n"; std::cout.flush();
    clear();
    //line_author->setText(QString(paper->authors().Get(0).c_str()));
    for (int i=0; i<paper->authors().size(); i++) {
        new_author = new QListWidgetItem(paper->authors().Get(i).c_str());
        list_author->addItem(new_author);
    }
    line_title->setText(QString(paper->title().c_str()));
    text_annote->setText(QString(paper->annote().c_str()));
    if (paper->has_attachment()) {
        
        std::cout << "ATTACHID VIEWD: " << paper->attachment() << " HasAttachment: " << paper->has_attachment() << "\n";
        /*std::string s;
        std::stringstream out;
        out << paper->attachment();
        s = out.str();*/
        biblio::Attachment attachment;
        db->get_attachment(&attachment, *paper);
        //QString s;
        //s.setNum(paper->attachment());
        line_attachment->setText(QString(attachment.blob().c_str()));
    } else {
        std::cout << "No attachment\n"; std::cout.flush();
    }
}

void MainWin::on_button_left_clicked()
{
    biblio::Paper paper;
    int ret;
    std::cout << "Previous data\n";
    std::cout.flush();
    
    if (view_state == ViewState_CHANGED or view_state == ViewState_NEW_CHANGED) {
        DataChangedDialog *dialog = new DataChangedDialog;
        dialog->exec();
        ret = dialog->get_clicked_button();
        if (ret == DialogReturn_CANCEL) {
            return;
        } else if (ret == DialogReturn_NO) {
            clear();
        } else if (ret == DialogReturn_YES) {
            save();
        }
    }
    
    ret = db->previousData(&paper);
    std::cout << "Previous data finished" << ret << "\n"; std::cout.flush();
    
    if (!ret) {
        //std::cout << paper.autors() << "\n";
        std::cout << paper.title() << "\n";
        std::cout << paper.annote() << "\n";
        std::cout.flush();
        load_data(&paper);
        std::cout << "After load_data\n"; std::cout.flush();
        view_state = ViewState_UNCHANGED;
    } else {
     
        QTextStream qout(stdout);
        qout << "Error previous" << endl;
        /*line_author->setText(QString(""));
        line_title->setText(QString(""));
        text_annote->setText(QString(""));*/
    }

}

void MainWin::on_button_open_clicked()
{
    QFileDialog file_dialog;
    if (file_dialog.exec()) {
        QStringList file_list;
        QTextStream qout(stdout);
        
        change();
        file_list = file_dialog.selectedFiles();
        
        qout << file_list.first() << endl;
        line_attachment->setText(file_list.first());
        std::cout << "Dialog ok ok" << file_list.length() << "\n";
        std::cout.flush();
    }
}

void MainWin::on_button_view_clicked()
{
    std::cout << "View \n";
    std::system("evince &");
    std::cout.flush();
}
void MainWin::on_button_right_clicked()
{
    biblio::Paper paper;
    int ret;
    std::cout << "Next data\n";
    std::cout.flush();
    
    if (view_state == ViewState_CHANGED or view_state == ViewState_NEW_CHANGED) {
        DataChangedDialog *dialog = new DataChangedDialog;
        dialog->exec();
        ret = dialog->get_clicked_button();
        if (ret == DialogReturn_CANCEL) {
            return;
        } else if (ret == DialogReturn_NO) {
            clear();
        } else if (ret == DialogReturn_YES) {
            save();
        }
    }
    
    ret = db->nextData(&paper);

    if (!ret) {
        //std::cout << paper.getAuthor() << "\n";
        setWindowTitle(QString("biblio - ") + QString(paper.title().c_str()));
        std::cout << paper.title() << "\n";
        std::cout << paper.annote() << "\n";
        std::cout.flush();
        load_data(&paper);
        view_state = ViewState_UNCHANGED;
    } else {
        setWindowTitle(QString("biblio - keyerr"));
        QTextStream qout(stdout);
        qout << "Error next" << endl;
        /*line_author->setText(QString(""));
        line_title->setText(QString(""));
        text_annote->setText(QString(""));*/
    }
}

void MainWin::on_line_author_textEdited()
{
    change();
}

void MainWin::on_line_title_textEdited()
{
    change();
}

void MainWin::on_text_annote_textChanged()
{
    change();
}

void MainWin::change()
{
    if (view_state == ViewState_NEW_UNCHANGED) {
        view_state = ViewState_NEW_CHANGED;
    } else if (view_state == ViewState_UNCHANGED) {
        view_state = ViewState_CHANGED;
    }
}


void MainWin::on_button_save_clicked()
{
    if (view_state == ViewState_CHANGED ) {
        save();
        view_state = ViewState_UNCHANGED;
    } else if (view_state == ViewState_NEW_CHANGED) {
        save();
        view_state = ViewState_NEW_UNCHANGED;
    }
}

MainWin::~MainWin() {
    if (db) {
        delete db;
    }
}

void MainWin::save()
{
    biblio::Paper paper;
    for (int i=0; i<list_author->count(); i++) {
        paper.add_authors(list_author->item(i)->text().toStdString());
    }
    //paper.add_authors(line_author->text().toStdString());
    paper.set_title(line_title->text().toStdString());
    paper.set_annote(text_annote->toPlainText().toStdString());
    if (view_state == ViewState_NEW_CHANGED) {
        db->saveNewData(&paper);
    } else if (view_state == ViewState_CHANGED) {
        db->saveData(&paper);
    }
}

void MainWin::clear()
{
    line_title->clear();
    list_author->clear();
    line_author->clear();
    line_title->clear();
    line_attachment->clear();
    text_annote->clear();
}

void MainWin::on_button_add_author_clicked()
{
    std::cout << "Add clicked\n";
    std::cout.flush();
    QListWidgetItem *new_author = new QListWidgetItem(line_author->text());
    list_author->addItem(new_author);
    line_author->clear();
}

void MainWin::on_button_new_clicked()
{
    int ret;
    std::cout << "New clicked\n";
    std::cout.flush();
    if (view_state == ViewState_CHANGED || view_state == ViewState_NEW_CHANGED) {
        DataChangedDialog *dialog = new DataChangedDialog;
        dialog->exec();
        ret = dialog->get_clicked_button();
        if (ret == DialogReturn_CANCEL) {
            return;
        } else if (ret == DialogReturn_NO) {
            
        } else if (ret == DialogReturn_YES) {
            save();
        }
    }
    clear();
    view_state = ViewState_NEW_UNCHANGED;
}

#include "mainwin.moc"
